
import traceback

from flask import Blueprint, g
from flask_restful import Resource, Api, reqparse, marshal

from common.models.account import Channel, User
from common import db, cache
from common.model_fields.model_fields import channel_fields
from common.utils.jwt_utils import create_login_token
from common.utils.login_util import login_required

channel_bp = Blueprint('channel_bp', __name__)
api = Api(channel_bp)


class AddChannel(Resource):
    """
    添加频道
    """
    @login_required
    def post(self):
        user_id = g.user_id
        user = User.query.get(user_id)
        if user.superuser != 1:
            return {'message': 'not permission', 'code': 403}

        parser = reqparse.RequestParser()
        parser.add_argument('cname')
        args = parser.parse_args()
        cname = args.get('cname')

        channel = Channel(cname=cname)
        db.session.add(channel)
        db.session.commit()
        return {'message': 'ok', 'code': 200, 'data': marshal(channel, channel_fields)}


api.add_resource(AddChannel, '/add_channel', endpoint='add_channel')

